/**
 * Copyright (c) aunigold, Inc All Rights Reserved.
 */

package com.longau.guinea.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ConfigurationUtil
 * @author GaoKun
 * @version 1.0, Created on 2013年11月26日
 */
public class ConfigurationUtil {
	protected final static Logger logger = LoggerFactory
			.getLogger(ConfigurationUtil.class);


	@SuppressWarnings("rawtypes")
	private static Map propertyKeyMap = new HashMap();
	@SuppressWarnings("rawtypes")
	private static Map propertiesMap = new LinkedHashMap();
	private static PropertiesConfiguration config;

	static {
		try {
			init();
		} catch (ConfigurationException e) {
			logger.error(e.getMessage());
		}
	}

	/**
	 * 初始化Config
	 * 
	 * @throws ConfigurationException
	 */
	static void init() throws ConfigurationException {
		
		config = new PropertiesConfiguration();
		config.setEncoding("UTF-8");
//		config.setDelimiterParsingDisabled(true);
		config.load("config.properties");
	}
	
	public static void setDelimiterParsingDisabled(boolean disabled){
		config.setDelimiterParsingDisabled(disabled);
	}

	/**
	 * 带缺省值的读取属性
	 * 
	 * @param key
	 * @param defalutValue
	 * @return
	 */
	public static String getProperty(String key, String defalutValue) {
		return config.getString(key, defalutValue);
	}

	public static String getProperty(String key) {
		return config.getString(key);
	}

	public static int getPropertyForInt(String key, int defaultValue) {
		return config.getInt(key, defaultValue);
	}

	public static int getPropertyForInt(String key) {
		return config.getInt(key);
	}

	public static boolean getPropertyForBoolean(String key, Boolean defaultValue) {
		return config.getBoolean(key, defaultValue).booleanValue();
	}

	public static boolean getPropertyForBoolean(String key) {
		return config.getBoolean(key);
	}

	public static double getPropertyForDouble(String key, double defaultValue) {
		return config.getDouble(key, defaultValue);
	}

	public static double getPropertyForDouble(String key) {
		return config.getDouble(key);
	}

	public static long getPropertyForLong(String key, long defaultValue) {
		return config.getLong(key, defaultValue);
	}

	public static long getPropertyForLong(String key) {
		return config.getLong(key);
	}

	public static String[] getStringArray(String key) {
		return config.getStringArray(key);
	}

	@SuppressWarnings("rawtypes")
	public static Map getProperties() {
		Iterator it = config.getKeys();
		List keyList = new ArrayList();

		while (it.hasNext()) {
			String key = (String) it.next();
			keyList.add(key);
		}

		Collections.sort(keyList);
		it = keyList.iterator();

		while (it.hasNext()) {
			String key = (String) it.next();
			//String value = config.getString(key);//config.getList(key)
			String[] values=config.getStringArray(key);
			if(values.length==1){
				String value = config.getString(key);
				propertiesMap.put(key, value);
			}else{
				StringBuffer sb = new StringBuffer();
				for(int i = 0; i < values.length; i++){
				 sb.append(values[i]+",");
				}
				String value = sb.toString().substring(0,sb.length()-1);
				propertiesMap.put(key, value);
			}
			
		}

		return propertiesMap;
	}

	public static void save(String key, String value)
			throws ConfigurationException {
		config.setProperty(key, value);
		config.save("config.properties");
	}

	@SuppressWarnings("rawtypes")
	public static void saveAll(Map params) throws ConfigurationException {
		Iterator it = params.entrySet().iterator();

		while (it.hasNext()) {
			Entry entry = (Entry) it.next();
			config.setProperty(entry.getKey().toString(), entry.getValue());
		}
		config.setFileName("config.properties");
		config.save();
	}

	@SuppressWarnings("rawtypes")
	public static Map propertyKeyMap() {
		return propertyKeyMap;
	}

	public static boolean contains(String key) {
		return config.containsKey(key);
	}
}
